/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.demo.consumer.comp.DemoServiceComponent;

import org.apache.dubbo.demo.consumer.comp.MockComponent;
import org.apache.dubbo.demo.consumer.comp.MyGenericServiceComponent;
import org.apache.dubbo.demo.consumer.comp.StubComponent;
import org.springframework.context.annotation.*;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;


public class ApplicationC {
    /**
     * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
     * launch the application
     */
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        //阻塞在哪里
        /*CompletableFuture.runAsync(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            DemoService service = context.getBean("demoServiceComponent", DemoServiceComponent.class);
            System.out.println(service.sayHello("world"));
        });*/

        new Thread(() -> {
            System.out.println(Thread.currentThread().isDaemon());
            DemoService service = context.getBean("demoServiceComponent", DemoServiceComponent.class);
            System.out.println(service.sayHello("world"));
            CompletableFuture<String> s = service.sayHelloAsync("恐怖如斯");
            s.whenComplete((v, t) -> {
                //其他线程调用
                System.out.println(Thread.currentThread().getName());
                System.out.println("调用完毕");
            });
            System.out.println(Thread.currentThread().getName());
            System.out.println("你确定是异步吗？");
        }).start();
        //泛化调用
       /* MyGenericServiceComponent genericServiceComponent = context.getBean("genericServiceComponent", MyGenericServiceComponent.class);
        genericServiceComponent.d1();

        StubComponent stubComponent = context.getBean("stubComponent", StubComponent.class);
        stubComponent.d1();
        MockComponent mockComponent = context.getBean("mockComponent", MockComponent.class);
        mockComponent.d1();*/
        System.in.read();
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.consumer.comp")
    @PropertySource("classpath:/spring/dubbo-consumer.properties")
    @ComponentScan(value = {"org.apache.dubbo.demo.consumer.comp"})
    static class ConsumerConfiguration {
        @Bean
        public RegistryConfig registryConfig() {
            RegistryConfig registryConfig = new RegistryConfig();
            //添加连接时间
            registryConfig.setTimeout(300000);
            registryConfig.setAddress("zookeeper://122.51.148.202:2181");
            return registryConfig;
        }
    }
}
